﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard.Settle
{
    public partial class Settle_check : System.Web.UI.Page
    {
        string settle_id;
        string strUserName = "";
        string strUserID = "";
        string strEntity = "";
        public string strScript = "";
        public string card_balance = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["settle_id"] != null)
            {
                settle_id = Request["settle_id"].ToString();
            }

            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }

            if (!Page.IsPostBack)
            {
                bindDetailInfo(int.Parse(settle_id));
            }
        }

        private void bindDetailInfo(int settle_id)
        {
            try
            {
                DataSet ds = netBankDao.getDealAllInfo_bySettleID(settle_id);
                //客户信息
                rptCparty.DataSource = ds.Tables[0].DefaultView;
                rptCparty.DataBind();
                //交易信息
                rptDeals.DataSource = ds.Tables[1].DefaultView;
                rptDeals.DataBind();
                //结算信息
                rptSettleDeals.DataSource = ds.Tables[2].DefaultView;
                rptSettleDeals.DataBind();                
                //附件
                rptAttach.DataSource = ds.Tables[3].DefaultView;
                rptAttach.DataBind();
                //资金流
                gridCflows1.DataSource = ds.Tables[4].DefaultView;
                gridCflows1.DataBind();

                if (ds.Tables[5] != null && ds.Tables[5].Rows.Count > 0)
                {
                    card_balance = ds.Tables[5].Rows[0]["card_balance"].ToString();
                }

                if (ds.Tables[6] != null && ds.Tables[6].Rows.Count > 0)
                {
                    gridTransfer1.DataSource = ds.Tables[6].DefaultView;
                    gridTransfer1.DataBind();
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //settle_check_pass
            string pass_remark=TextBox1.Text.Trim();
            //检查一下是否有多笔在申请中的情况。
            if (DealDao.check_ifRepeat_requestSettle(settle_id))
            {
                KVB.Common.Log.WriteDefaultLog("结算复核审核：",settle_id +"存在多笔未结束的结算申请。");
                strScript = "<script>alert('审核失败！settle id :" + settle_id + ":改笔原始交易存在多笔未结束的结算申请！请联系管理员');</script>";
            }
            else
            {
                DealDao.settle_check_pass(settle_id, strUserID, pass_remark);
                //KVB.Common.JavaScript.ViewAlertMsg("审核通过");
                strScript = "<script>alert('审核成功！'); window.opener.refresh();window.opener=null;window.close();</script>";
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string back_reason = TextBox2.Text.Trim();
            DealDao.settle_check_back(settle_id, strUserID, back_reason);
            //KVB.Common.JavaScript.ViewAlertMsg("退回成功");
            strScript = "<script>alert('退回成功！'); window.opener.refresh();window.opener=null;window.close();</script>";
        }
    }
}